package edu.mtu.citizenscience.cs.notepad.activity.noteslist.note.recording;

import java.io.IOException;
import java.util.Calendar;

import android.os.Bundle;
import android.os.SystemClock;
import android.widget.Chronometer;

import edu.mtu.citizenscience.cs.notepad.NPActivity;
import edu.mtu.citizenscience.cs.notepad.activity.R;
import edu.mtu.citizenscience.cs.notepad.model.ModelNote;
import edu.mtu.citizenscience.cs.utils.Audio;
import edu.mtu.citizenscience.cs.utils.Message;
import edu.mtu.citizenscience.cs.utils.exceptions.NoExternalWrite;
/**
 * @author Corbin Uselton <corbinu@cwuselto@mtu.edu>
 * @copyright (c) 2012 MichiganTech <http://www.mtu.edu>
 * @since 1.0
 * 
 * Recording Activity Responsible taking, playing and deleting the note recording
 */
public class RecordActivity extends NPActivity {
	
	// The model of the note the recording gets attached to
	private ModelNote note;
	
	/**
	 * Record Activity creation handler
	 * 
	 * @param   savedInstanceState - Bundle - Bundle with launch info
	 * @return	void
	 */
	@Override
	public void onCreate(Bundle savedInstanceState) 
	{
		super.onCreate(savedInstanceState);
		
		this.setContentView(R.layout.activity_record);
		
		// try to get the note id from the intent bundle
		long noteId = this.getIntent().getLongExtra(ModelNote.NOTE_ID, -1);
		
		// check to see if got a note id
		if (noteId > -1)
		{
			// set the note model to the model with the given note id
			this.note = new ModelNote(this, noteId).find();
		}
		else
		{
			// no note to attach the recording to so finish the activity
			this.finish();
		}
		
		// set the recording end button click handler to stopRecording
		this.aq.id(R.id.recordEndButton).clicked(this, "stopRecording");
		// start recording
		this.doRecord();
	}
    
	/**
	 * Record Activity pause handler
	 * 
	 * @param   void
	 * @return	void
	 */
    @Override
	public void onPause()
	{
		super.onPause();
		
		// detach the recorder
		Audio.unloadRecorder();
	}
	
    /**
	 * Make a new recording
	 * 
	 * @param   void
	 * @return	void
	 */
	public void doRecord()
	{
		try
		{
			// get the chronometer
			Chronometer meter = (Chronometer) findViewById(R.id.recordingChronometer);
			// reset the chornometer to 0
			meter.setBase(SystemClock.elapsedRealtime());
			// start the meter
			meter.start();
			// set the recordings start time to the current date-time
			this.note.getRecording().setStartDate(Calendar.getInstance().getTime());
			// start recording and set the recording path to the returned recording file's path
			this.note.getRecording().setPath(Audio.record(this, "notepad").getAbsolutePath());
		}
		catch (NoExternalWrite e)
		{
			Message.showMessage(this, "Error: External storage is not writable");
		} 
		catch (IllegalStateException e) 
		{
			e.printStackTrace();
		} 
		catch (IOException e) 
		{
			e.printStackTrace();
		}
	}
	
	/**
	 * onClick handler for stop recording
	 * 
	 * @param   void
	 * @return	void
	 */
	public void stopRecording()
	{
		// detach the recorder
		Audio.unloadRecorder();
		
		// stop the meter
		((Chronometer) findViewById(R.id.recordingChronometer)).stop();
		
		// set the recording end date as the current date-time
		this.note.getRecording().setEndDate(Calendar.getInstance().getTime());
		// set the recording note as the note model
		this.note.getRecording().setNote(this.note);
		// save the recording
		this.note.getRecording().save();
		
		// finished recording so finish the activity
		this.finish();
	}
}